<?php include('php/authenticatephastregex.php'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
  <head>
    <title>Phastregex. in-silico chip-seq</title>
    <meta http-equiv="Content-Type" content ="text/html; charset=utf-8">
    <link rel="stylesheet" href="phastregexstylejw.css" type="text/css">
    <?php include('php/jasonweirather.php'); ?>
  </head>
  <body>
    <?php include('php/logoline.php'); ?>
    <div id = "hit_results_block">
    <?php
      $regex = $_POST["regex"];
      $species = $_POST["species"];
      $title = $_POST["title"];      
      $regex_id = $_POST["regex_id"];      
      echo("$title<br>");
      echo("Species genome: $species<br>");
      echo("Regular expression: $regex<br>");
      ### check to see if it is in the database and completed already.
      $database = 'zebraphish';
      include("php/dbconnect.php");
      $_SESSION['basename'] = NULL;
      include("php/filehandles.php");
      $conservedfile = "$basename" . "_cons.txt";
      $cons_level = $_POST['conservation_level'];
      $cons_db = $_POST['conservation_db'];
      # we need to generate the conservation file
      if($cons_level == 'any' || $cons_db == 'any' || $cons_level == NULL || $cons_db == NULL) { 
        echo "generating annotated hit file for all regular expression $regex_id entries<br>\n";
        #$command = "$phastregex2path/print_conserved_hits.pl $regex_id > $workingdirectory/$conservedfile";
        #$command = "$phastregex2path/print_cons.pl $regex_id > $workingdirectory/$conservedfile";
        $command = "$phastregex2path/print_cons.pl $regex_id > $workingdirectory/$conservedfile";
        echo "executing: $command <br>\n";
        #echo "$command<br>\n";
        flush();
        sleep(1);
##MAJOR PAIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! see note below
        $lines = `$command`;
        #$output = exec($command, $output, $retval);
        #var_dump($output);
      } else {
        #make a conservation file to feed it. 
        $cparamfilename = "$workingdirectory/$basename.cparam";
        $fh = fopen($cparamfilename,'w') or die ("can't open file");
        $cparam = "_$cons_db\t$cons_level";
        fwrite($fh,$cparam);
        fclose($fh);
        $table = $species . "_" . $cons_db . "_" . $cons_level;
        echo "filtering based on conservation database $table<br>\n";
        $command = "cat $cparamfilename | $phastregex2path/print_cons.pl $regex_id true > $workingdirectory/$conservedfile";
        echo "executing: $command<br>\n";
        flush();
        sleep(1);
##MAJOR PAIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! see note below
#### we had a problem with the UCSChooks package or something and ended up having to write a wrapper around print_cons.pl so you will have
#### to probably set some path informatino inside print_cons.pl and print_cons_wrapper.pl and where it points tothe library for UCSChooks.  owned
        $lines = `$command`;
      }
      $_SESSION['conserved_file_generated'] = 0; #for testing
      echo("Conserved hits are availible for download:<br>");
      echo("<a href = \"$working_address/$conservedfile\">$working_address/$conservedfile</a><br>");
     echo('<form enctype="multipart/form-data" action="annotate.php" method="post">Add a feature and or filter this file further:<br>');
     echo("<input type=\"hidden\" name=\"local_file_name\" value=\"$conservedfile\"/>");
     echo('<input type="submit" value="add feature"/>');
     echo('</form>');
    # print the conserved file here.

    ?>
  </body>
    <?php include('php/lastline.php'); ?>
</html>
